<template>
  <view class="container">
    <view class="content">
      <view class="welcome">
        <text class="title">睡眠健康小助手</text>
        <text class="subtitle">让您的睡眠更健康、更美好</text>
      </view>

      <view class="menu-grid">
        <view class="menu-item" @click="navigateTo('/pages/survey/index')">
          <view class="menu-icon survey-icon">
            <text class="menu-icon-text">📋</text>
          </view>
          <text class="menu-text">问卷调查</text>
        </view>
        <view class="menu-item" @click="navigateTo('/pages/record/index')">
          <view class="menu-icon record-icon">
            <text class="menu-icon-text">✅</text>
          </view>
          <text class="menu-text">睡眠记录</text>
        </view>
        <view class="menu-item" @click="navigateTo('/pages/knowledge/index')">
          <view class="menu-icon knowledge-icon">
            <text class="menu-icon-text">📚</text>
          </view>
          <text class="menu-text">睡眠知识</text>
        </view>
        <view class="menu-item" @click="navigateTo('/pages/my/index')">
          <view class="menu-icon my-icon">
            <text class="menu-icon-text">🤖</text>
          </view>
          <text class="menu-text">AI对话</text>
        </view>
      </view>

      <view class="feature-section">
        <text class="section-title">最新文章</text>
        <view class="article-card" v-for="(item, index) in latestArticles" :key="index" @click="viewArticleDetail(item)">
          <image :src="processImageUrl(item.coverImage || defaultCover)" class="article-image" mode="aspectFill"></image>
          <view class="article-info">
            <text class="article-title text-ellipsis-2">{{ item.title }}</text>
            <text class="article-author">{{ item.author }}</text>
          </view>
        </view>
        <view class="more-btn" @click="navigateTo('/pages/knowledge/index')">
          <text>查看更多</text>
          <text class="more-icon">→</text>
        </view>
      </view>
    </view>

    <!-- 底部导航栏 -->
    <view class="tab-bar">
      <view class="tab-item active" @click="handleTabClick('home')">
        <text class="tab-icon" style="color: #2979ff;">🏠</text>
        <text class="tab-text" style="color: #2979ff;">首页</text>
      </view>
      <view class="tab-item" @click="handleTabClick('survey')">
        <text class="tab-icon">📋</text>
        <text class="tab-text">问卷</text>
      </view>
      <view class="tab-item" @click="handleTabClick('record')">
        <text class="tab-icon">✅</text>
        <text class="tab-text">记录</text>
      </view>
      <view class="tab-item" @click="handleTabClick('knowledge')">
        <text class="tab-icon">📚</text>
        <text class="tab-text">知识</text>
      </view>
      <view class="tab-item" @click="handleTabClick('my')">
        <text class="tab-icon">👤</text>
        <text class="tab-text">我的</text>
      </view>
    </view>
  </view>
</template>

<script>
console.log('首页 script 加载了');
import articleApi from '../../api/article';
import mixin from '../../utils/mixin';

export default {
  name: 'Index',
  mixins: [mixin],
  data() {
    return {
      defaultCover: 'http://localhost:9001/image/article/67fce526-83ba-49ef-9d14-86ab9a803a2b.png',
      latestArticles: []
    };
  },
  onLoad() {
    console.log('首页 - onLoad生命周期被调用');
    this.getLatestArticles();
  },
  onShow() {
    console.log('首页 - onShow生命周期被调用');
  },
  methods: {
    // 获取最新文章
    async getLatestArticles() {
      try {
        console.log('首页 - 开始获取最新文章');
        const res = await articleApi.getArticleList(1, 3);
        console.log('首页 - 后端返回数据:', res);
        
        if (res && res.code === 1 && res.data) {
          // 根据后端数据结构，文章列表在res.data.list中
          let articles = [];
          if (res.data.list && Array.isArray(res.data.list)) {
            articles = res.data.list;
          } else if (Array.isArray(res.data)) {
            articles = res.data;
          } else if (res.data.articles && Array.isArray(res.data.articles)) {
            articles = res.data.articles;
          }
          
          console.log('首页 - 解析后的文章数据:', articles);
          
          // 处理图片URL，添加完整的服务器地址
          articles = articles.map(item => {
            let cover = item.coverImage || item.cover_image || '';
            if (cover && !/^https?:\/\//.test(cover)) {
              // 如果不是http开头，补全为完整的服务器URL
              if (!cover.startsWith('/image')) {
                cover = '/image' + cover.replace(/^\/static/, '');
              }
              // 添加完整的服务器地址
              cover = 'http://localhost:9001' + cover;
            }
            return {
              ...item,
              coverImage: cover,
              title: item.title || item.articleTitle,
              author: item.author || '未知作者'
            };
          });
          
          this.latestArticles = articles;
        } else {
          console.error('首页 - API返回错误:', res);
        }
      } catch (error) {
        console.error('首页 - 获取最新文章失败:', error);
      }
    },

    // 查看文章详情
    viewArticleDetail(item) {
      uni.navigateTo({
        url: `/pages/knowledge/detail?id=${item.id}`
      });
    },

    // 页面跳转
    navigateTo(url) {
      uni.navigateTo({
        url: url
      });
    },

    // 处理标签点击
    handleTabClick(tab) {
      console.log('首页 - handleTabClick called with:', tab);
      switch (tab) {
        case 'home':
          // 当前已在首页，不需要跳转
          console.log('首页 - Already on home page');
          break;
        case 'survey':
          console.log('首页 - Navigating to survey page');
          uni.navigateTo({ url: '/pages/survey/index' });
          break;
        case 'record':
          console.log('首页 - Navigating to record page');
          uni.navigateTo({ url: '/pages/record/index' });
          break;
        case 'knowledge':
          console.log('首页 - Navigating to knowledge page');
          uni.navigateTo({ url: '/pages/knowledge/index' });
          break;
        case 'my':
          console.log('首页 - Navigating to my page');
          uni.navigateTo({ url: '/pages/my/index' });
          break;
      }
    }
  }
};
</script>

<style>
.container {
  display: flex;
  flex-direction: column;
  height: 100%;
  background-color: #f5f5f5;
}

.content {
  flex: 1;
  padding: 30rpx;
  padding-bottom: 120rpx;
}

.welcome {
  text-align: center;
  padding: 40rpx 0;
}

.title {
  font-size: 38rpx;
  font-weight: bold;
  color: #333;
}

.subtitle {
  font-size: 26rpx;
  color: #666;
  margin-top: 10rpx;
  display: block;
}

.menu-grid {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  margin: 20rpx 0 50rpx 0;
}

.menu-item {
  width: 48%;
  height: 180rpx;
  background-color: #fff;
  border-radius: 16rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  box-shadow: 0 2rpx 20rpx rgba(0, 0, 0, 0.05);
  margin-bottom: 30rpx;
}

.menu-icon {
  width: 100rpx;
  height: 100rpx;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 16rpx;
}

.survey-icon {
  background-color: rgba(41, 121, 255, 0.1);
}

.record-icon {
  background-color: rgba(7, 193, 96, 0.1);
}

.knowledge-icon {
  background-color: rgba(255, 149, 0, 0.1);
}

.my-icon {
  background-color: rgba(255, 90, 95, 0.1);
}

.menu-text {
  font-size: 28rpx;
  color: #333;
}

.feature-section {
  margin-top: 30rpx;
}

.section-title {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
  margin-bottom: 20rpx;
  display: block;
}

.article-card {
  display: flex;
  background-color: #fff;
  border-radius: 12rpx;
  overflow: hidden;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.article-image {
  width: 200rpx;
  height: 150rpx;
  flex-shrink: 0;
  background-color: #f5f5f5;
}

.article-info {
  padding: 16rpx;
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.article-title {
  font-size: 28rpx;
  font-weight: 500;
  color: #333;
  line-height: 1.4;
}

.article-author {
  font-size: 24rpx;
  color: #999;
  margin-top: 10rpx;
}

.more-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20rpx 0;
  color: #999;
  font-size: 26rpx;
}

.tab-bar {
  display: flex;
  justify-content: space-around;
  align-items: center;
  height: 100rpx;
  background-color: #fff;
  border-top: 1px solid #eee;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
}

.tab-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10rpx 0;
}

.tab-icon {
  font-size: 32rpx;
  color: #666;
  margin-bottom: 8rpx;
  line-height: 1;
}

.tab-text {
  font-size: 20rpx;
  color: #666;
  line-height: 1;
}

.tab-item.active .tab-text {
  color: #2979ff;
}

</style> 